package com.health.ts.controller.study;
import java.io.File;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.attachment.Attachment;
import com.health.ts.entity.study.Online;
import com.health.ts.entity.study.OnlineApply;
import com.health.ts.entity.study.StudyInfo;
import com.health.ts.entity.study.VOnline;
import com.health.ts.entity.study.VOnlineStudy;
import com.health.ts.entity.study.VStudyList;
import com.health.ts.exception.BusinessException;
import com.health.ts.service.attachment.AttachmentService;
import com.health.ts.service.study.OnlineApplyService;
import com.health.ts.service.study.OnlineService;
import com.health.ts.service.study.StudyInfoService;
import com.health.ts.service.study.VOnlineService;
import com.health.ts.service.study.VOnlineStudyService;
import com.health.ts.service.study.VStudyListService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;
/**
 * <p>
 *  远程教育管理
 * </p>
 *
 * @author ${author}
 * @since 2017-07-05
 */
@RestController
@RequestMapping(value = "/api/study",produces= BaseController.JSON_UTF8)
public class StudyController extends BaseController{
	
	@Autowired
	StudyInfoService studyInfoService;
	
	@Autowired
	AttachmentService attachmentService;
	
	@Autowired
	VStudyListService vstudyListService;
	
	@Autowired
	VOnlineStudyService  vonlineStudyService;
	
	@Autowired
	OnlineApplyService  onlineApplyService;
	
	@Autowired
	OnlineService  onlineService;
	
	@Autowired
	VOnlineService  vonlineService;
	
	@Value("${ts.upload.dir}")
    private String uploadDir;//保存路径
	
	
	/**
	 * 新增资源
	 * @param studyInfo
	 * @return
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult addStudy(@RequestBody StudyInfo studyInfo) {
		studyInfo.setId(GenerateIdUtil.makeID());
		studyInfo.setCreate_time(new Date());
		studyInfo.setOpr_docid(SessionUserUtil.getUserId());
		studyInfo.setOpr_name(SessionUserUtil.getUserName());
		studyInfo.setIsEnable("0");
		studyInfo.setIsAgree("0");
		//先上传
		studyInfoService.save(studyInfo);
		return ApiResult.success();
	}
	
	/**
	 * 审核学习资源通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/agree/{id}", method=RequestMethod.PUT)
	public ApiResult agreeStudy(@PathVariable String id) {
		StudyInfo studyInfo = studyInfoService.getById(id);
		AssertUtil.notNull(studyInfo, "要审核的学习资源不存在！");
		studyInfo.setIsAgree("1");//审核通过
		studyInfoService.updateById(studyInfo);
		return ApiResult.success();
	}
	
	/**
	 * 审核学习资源不通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/uagree/{id}", method=RequestMethod.PUT)
	public ApiResult uagreeStudy(@PathVariable String id) {
		StudyInfo studyInfo = studyInfoService.getById(id);
		AssertUtil.notNull(studyInfo, "要审核的学习资源不存在！");
		studyInfo.setIsAgree("2");//审核不通过
		studyInfoService.updateById(studyInfo);
		return ApiResult.success();
	}
	
	
	/**
	 * 删除资源
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	public ApiResult deleteStudy(@PathVariable String id) {
		StudyInfo study=studyInfoService.getById(id);
		study.setIsEnable("1");
		boolean isSucess=studyInfoService.updateById(study);
		if(isSucess){
			QueryWrapper<Attachment> QueryWrapper = new QueryWrapper<Attachment>();
			QueryWrapper.eq("business_id", study.getId());
			String filepath=attachmentService.getOne(QueryWrapper).getFilepath();
			if(attachmentService.remove(QueryWrapper)){//删除对应的文件
				File file = new File(filepath);
				if(file.exists()){
					file.delete();
				}else{
					throw new BusinessException("文件删除失败");
				}
			}
		}
		return ApiResult.success();
	}
	
	/**
	 * 查询资源
	 * @param Id
	 * @return
	 */
	@RequestMapping(value="/select/{Id}", method=RequestMethod.GET)
	public ApiResult selectStudy(@PathVariable String Id) {
		StudyInfo studyInfo=studyInfoService.getById(Id);
		AssertUtil.notNull(studyInfo, "要查询的资源不存在！");
		return ApiResult.success(studyInfo);
	}
	
	/**
	 * 查询列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public ApiResult queryList(@RequestBody FrontPage page) {
		QueryWrapper<VStudyList> QueryWrapper = new QueryWrapper<VStudyList>();
		String resource_name =getParam(page,"resource_name");
		if (StringUtils.isNotEmpty(resource_name)){
			QueryWrapper.like("resource_name", resource_name); 
		}
		String resource_category =getParam(page,"resource_category");
		if (StringUtils.isNotEmpty(resource_category)){
			QueryWrapper.eq("resource_category", resource_category); 
		}
		String resource_type =getParam(page,"resource_type");
		if (StringUtils.isNotEmpty(resource_type)){
			QueryWrapper.like("resource_type", resource_type); 
		}
		QueryWrapper.eq("isEnable", "0");
		Page<VStudyList> pageList = vstudyListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VStudyList> customPage = new CustomPage<VStudyList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 待审核
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listAgree", method=RequestMethod.POST)
	public ApiResult queryListAgree(@RequestBody FrontPage page) {
		QueryWrapper<VStudyList> QueryWrapper = new QueryWrapper<VStudyList>();
		String resource_name =getParam(page,"resource_name");
		if (StringUtils.isNotEmpty(resource_name)){
			QueryWrapper.like("resource_name", resource_name); 
		}
		String resource_category =getParam(page,"resource_category");
		if (StringUtils.isNotEmpty(resource_category)){
			QueryWrapper.eq("resource_category", resource_category); 
		}
		String resource_type =getParam(page,"resource_type");
		if (StringUtils.isNotEmpty(resource_type)){
			QueryWrapper.like("resource_type", resource_type); 
		}
		QueryWrapper.eq("isEnable", "0");
		QueryWrapper.eq("isAgree", "0");//待审核状态
		Page<VStudyList> pageList = vstudyListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VStudyList> customPage = new CustomPage<VStudyList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 查询可以学习的资源信息
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listView", method=RequestMethod.POST)
	public ApiResult queryListView(@RequestBody FrontPage page) {
		QueryWrapper<VStudyList> QueryWrapper = new QueryWrapper<VStudyList>();
		String resource_name =getParam(page,"resource_name");
		if (StringUtils.isNotEmpty(resource_name)){
			QueryWrapper.like("resource_name", resource_name); 
		}
		String resource_category =getParam(page,"resource_category");
		if (StringUtils.isNotEmpty(resource_category)){
			QueryWrapper.eq("resource_category", resource_category); 
		}
		String resource_type =getParam(page,"resource_type");
		if (StringUtils.isNotEmpty(resource_type)){
			QueryWrapper.like("resource_type", resource_type); 
		}
		QueryWrapper.eq("isEnable", "0");//未删除
		QueryWrapper.eq("isAgree", "1");//审核通过
		Page<VStudyList> pageList = vstudyListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VStudyList> customPage = new CustomPage<VStudyList>(pageList);
		return ApiResult.success(customPage);
	}
	
	
	@RequestMapping(value="/applyOnline", method=RequestMethod.POST)
	public ApiResult applyOnline(@RequestBody OnlineApply onlineApply) {
		onlineApply.setId(GenerateIdUtil.makeID());
		onlineApply.setApply_time(new Date());
		onlineApply.setApplydocid(SessionUserUtil.getUserId());
		onlineApply.setApplydid(SessionUserUtil.getUserDeptId());
		onlineApply.setApplyhid(SessionUserUtil.getUserHosId());
		onlineApply.setIsAgree("0");
		onlineApplyService.save(onlineApply);
		return ApiResult.success();
	}
	
	/**
	 * 查询申请资源
	 * @param Id
	 * @return
	 */
	@RequestMapping(value="/selectApply/{Id}", method=RequestMethod.GET)
	public ApiResult selectApply(@PathVariable String Id) {
		VOnlineStudy vonlineStudy=vonlineStudyService.getById(Id);
		AssertUtil.notNull(vonlineStudy, "要查询的资源不存在！");
		return ApiResult.success(vonlineStudy);
	}
	
	/**
	 * 查询在线申请
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listOnlineApply", method=RequestMethod.POST)
	public ApiResult queryOnlineApplyList(@RequestBody FrontPage page) {
		QueryWrapper<VOnlineStudy> QueryWrapper = new QueryWrapper<VOnlineStudy>();
		String user_name =getParam(page,"user_name");
		if (StringUtils.isNotEmpty(user_name)){
			QueryWrapper.like("user_name", user_name); 
		}
		String study_type =getParam(page,"study_type");
		if (StringUtils.isNotEmpty(study_type)){
			QueryWrapper.eq("study_type", study_type); 
		}
		Page<VOnlineStudy> pageList = vonlineStudyService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VOnlineStudy> customPage = new CustomPage<VOnlineStudy>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 课程申请审核
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listOnlineAgree", method=RequestMethod.POST)
	public ApiResult queryOnlineAgreeList(@RequestBody FrontPage page) {
		QueryWrapper<VOnlineStudy> QueryWrapper = new QueryWrapper<VOnlineStudy>();
		String user_name =getParam(page,"user_name");
		if (StringUtils.isNotEmpty(user_name)){
			QueryWrapper.like("user_name", user_name); 
		}
		String study_type =getParam(page,"study_type");
		if (StringUtils.isNotEmpty(study_type)){
			QueryWrapper.eq("study_type", study_type); 
		}
		QueryWrapper.eq("isAgree", "0");
		Page<VOnlineStudy> pageList = vonlineStudyService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VOnlineStudy> customPage = new CustomPage<VOnlineStudy>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 审核通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/agreeOnline/{id}", method=RequestMethod.PUT)
	public ApiResult agreeOnline(@PathVariable String id) {
		OnlineApply online=onlineApplyService.getById(id);
		AssertUtil.notNull(online, "要审核的课程申请不存在！");
		online.setIsAgree("1");//审核通过
		onlineApplyService.updateById(online);
		return ApiResult.success();
	}
	
	/**
	 * 审核不通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/unagreeOnline/{id}", method=RequestMethod.PUT)
	public ApiResult unagreeOnline(@PathVariable String id) {
		OnlineApply online=onlineApplyService.getById(id);
		AssertUtil.notNull(online, "要审核的课程申请不存在！");
		online.setIsAgree("2");//审核不通过
		onlineApplyService.updateById(online);
		return ApiResult.success();
	}
	
	/**
	 * 查询发布列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listShowOnline", method=RequestMethod.POST)
	public ApiResult queryShowOnlineList(@RequestBody FrontPage page) {
		QueryWrapper<VOnline> QueryWrapper = new QueryWrapper<VOnline>();
		String course_name =getParam(page,"course_name");
		if (StringUtils.isNotEmpty(course_name)){
			QueryWrapper.like("course_name", course_name); 
		}
		String study_type =getParam(page,"study_type");
		if (StringUtils.isNotEmpty(study_type)){
			QueryWrapper.eq("study_type", study_type); 
		}
		Page<VOnline> pageList = vonlineService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VOnline> customPage = new CustomPage<VOnline>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 保存课程发布信息
	 * @param online
	 * @return
	 */
	@RequestMapping(value="/showOnline", method=RequestMethod.POST)
	public ApiResult showOnline(@RequestBody Online online) {
		online.setId(GenerateIdUtil.makeID());
		online.setOperid(SessionUserUtil.getUserId());
		online.setOperdid(SessionUserUtil.getUserDeptId());
		online.setOperhid(SessionUserUtil.getUserHosId());
		online.setIsEnable("1");
		onlineService.save(online);
		return ApiResult.success();
	}
	
	/**
	 * 查看课程详情
	 * @param Id
	 * @return
	 */
	@RequestMapping(value="/selectOnline/{Id}", method=RequestMethod.GET)
	public ApiResult selectOnline(@PathVariable String Id) {
		Online online=onlineService.getById(Id);
		AssertUtil.notNull(online, "要查询的课程不存在！");
		return ApiResult.success(online);
	}
	
	/**
	 * 查询可以学习的资源信息
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listOnlineView", method=RequestMethod.POST)
	public ApiResult queryListOnlineView(@RequestBody FrontPage page) {
		QueryWrapper<VOnline> QueryWrapper = new QueryWrapper<VOnline>();
		String course_name =getParam(page,"course_name");
		if (StringUtils.isNotEmpty(course_name)){
			QueryWrapper.like("course_name", course_name); 
		}
		String study_type =getParam(page,"study_type");
		if (StringUtils.isNotEmpty(study_type)){
			QueryWrapper.eq("study_type", study_type); 
		}
		Page<VOnline> pageList = vonlineService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VOnline> customPage = new CustomPage<VOnline>(pageList);
		return ApiResult.success(customPage);
	}
}
